package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.SubAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class SubAreaAction extends BaseAction<SubArea> {
	
	@Autowired
	private SubAreaService subAreaService;

	private File file;	
	public void setFile(File file) {
		this.file = file;
	}


	//导入分区数据    subArea_importSubArea
	@Action(value="subArea_importSubArea",results={@Result(name="success",type="json")})
	public String importSubArea() throws  IOException{
		List<SubArea> list = new ArrayList<>();
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
		for (Row row : sheet) {
			if(row.getRowNum()==0){
				continue;
			}
			if(row.getCell(0)== null || StringUtils.isBlank(row.getCell(0).getStringCellValue())){
				continue;
			}
			//分区编号
			SubArea subArea = new SubArea();
			subArea.setId(row.getCell(0).getStringCellValue());
			//定区编号
			FixedArea fixedArea = new FixedArea(); 
			fixedArea.setId(row.getCell(1).getStringCellValue());
			subArea.setFixedArea(fixedArea);
			//区域编号
			Area area = new Area();
			area.setId(row.getCell(2).getStringCellValue());
			subArea.setArea(area);
			
			subArea.setKeyWords(row.getCell(3).getStringCellValue());// 关键字
			subArea.setStartNum(row.getCell(4).getStringCellValue());// 初始化
			subArea.setEndNum(row.getCell(5).getStringCellValue());// 结束号
			
			subArea.setSingle(row.getCell(6).getStringCellValue().toCharArray()[0]);// 单双号
			subArea.setAssistKeyWords(row.getCell(7).getStringCellValue());// 辅助关键字（位置信息）
			
			
			list.add(subArea);
			
		}
		subAreaService.save(list);
		
		
		return SUCCESS;
	}
	
}
